package com.safeluck.scannerqcr.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.safeluck.scannerqcr.R;
import com.safeluck.scannerqcr.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * ScannerQCR
 * Created by lzw on 2017/12/5. 14:01:19
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MyMenuHodler> {

    private List<String> menus = new ArrayList<>();
    private LayoutInflater mInflater;
    private OnItemClickListener mListener;
    private SparseBooleanArray s;
    private int mLastCheckedPosition = -1;
    private Context mContext;

    @Override
    public MyMenuHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.menu_item,parent,false);
        final MyMenuHodler myMenuHodler = new MyMenuHodler(view);
        myMenuHodler.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.onItemClick(myMenuHodler.getAdapterPosition(),myMenuHodler.itemView,myMenuHodler);
                }
            }
        });
        return myMenuHodler;
    }


    public void setOnItemClickListener(OnItemClickListener listener){
        this.mListener = listener;
    }

    @Override
    public void onBindViewHolder(MyMenuHodler holder, int position) {
        String str = menus.get(position);
        if (s.get(position)){
            holder.viewline.setVisibility(View.VISIBLE);

            holder.itemView.setBackgroundColor(Color.WHITE);
            holder.textView.setTextColor(mContext.getResources().getColor(R.color.colorAccent));
        }else{
            holder.viewline.setVisibility(View.INVISIBLE);
            holder.itemView.setBackgroundResource(R.color.bg_app);
            holder.textView.setTextColor(Color.BLACK);

        }
        holder.textView.setText(str);
    }

    public MenuAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    @Override
    public int getItemCount() {
        return menus.size();
    }

    public void setDatas(List<String> strs){
        menus.clear();
        menus.addAll(strs);

        s = new SparseBooleanArray(strs.size());

    }

    public void setItemChecked(int itemChecked) {
        s.put(itemChecked,true);
        if (mLastCheckedPosition>-1){
            s.put(mLastCheckedPosition,false);
            notifyItemChanged(mLastCheckedPosition);
        }
        notifyDataSetChanged();
        mLastCheckedPosition = itemChecked;
    }

    class MyMenuHodler extends RecyclerView.ViewHolder{

        private TextView textView;
        private View viewline;

        public MyMenuHodler(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_menu);
            viewline = itemView.findViewById(R.id.view_line);
        }
    }
}
